﻿using System.Collections.ObjectModel;
using Inovout.Gui.Wpf.ViewModels;
using System.ComponentModel;
using System.Windows.Data;
using System;
using System.Linq;
using Inovout.Business;
using System.Windows.Documents;
using System.Collections.Generic;
using Inovout.Gui.Wpf;

namespace Inovout.Portal.Desktop.Workbenchs
{
    public class MainWorkbenchViewModel : BaseWorkbenchViewModel
    {

        //soapboxcore.2010.11.29.zip
        //AvalonMVVMBehavior
        //private ObservableCollection<MenuItemViewModel> menus;
        //public ObservableCollection<MenuItemViewModel> Menus
        //{
        //    get
        //    {
        //        if (menus == null)
        //        {
        //            menus = new ObservableCollection<MenuItemViewModel>(MenuDescriptor.Build());
        //        }
        //        return menus;
        //    }
        //}

        private ObservableCollection<DomainViewModel> documents;
        public ObservableCollection<DomainViewModel> Documents
        {
            get
            {
                return documents;
            }
        }
        internal void AddToDocument(DomainViewModel vm)
        {
            documents.Add(vm);
            documentView.MoveCurrentToLast();
        }
        private ObservableCollection<DomainViewModel> pads;
        public ObservableCollection<DomainViewModel> Pads
        {
            get
            {
                return pads;
            }
        }
        internal void AddToPad(DomainViewModel vm)
        {
            pads.Add(vm);
            padView.MoveCurrentToLast();
        }
        private ICollectionView documentView;
        private ICollectionView padView;
        public MainWorkbenchViewModel()
        {
            documents = new ObservableCollection<DomainViewModel>();
            pads = new ObservableCollection<DomainViewModel>();
            documentView = CollectionViewSource.GetDefaultView(documents);
            documentView.CurrentChanged += new EventHandler(Workspace_CurrentChanged);
            padView = CollectionViewSource.GetDefaultView(pads);
            padView.CurrentChanged += new EventHandler(Workspace_CurrentChanged);
        }


        protected virtual void Workspace_CurrentChanged(object sender, EventArgs e)
        {
            ListCollectionView workspaceView = sender as ListCollectionView;
            if (workspaceView.CurrentPosition > -1)
            {
                this.ActivedWorkspace = workspaceView.CurrentItem as DomainViewModel;
            }
        }

        internal  BaseCommand GetCommand(Function function)
        {
            List<DomainViewModel> viewModels = new List<DomainViewModel>();
            viewModels.AddRange(documents);
            viewModels.AddRange(pads);

            DomainViewModel modelViewModel = viewModels.SingleOrDefault(vm =>
                   vm.ViewContext.WorkItemContext.WorkItem.GetType().Equals(function.WorkItemType));

            if (modelViewModel == null)
            {
                modelViewModel = viewModels.SingleOrDefault(vm =>
                                vm.ViewContext.Function.Functions.Contains(function));
                if (modelViewModel == null)
                {
                    return null;
                }
                else
                {
                    return modelViewModel.GetCommand(function);
                }
            }
            else
            {
                return new ActiveWorkspaceCommand(this, modelViewModel);
            }
        }

        internal void ActiveWorkspace(DomainViewModel modelViewModel)
        {
            if (!documentView.MoveCurrentTo(modelViewModel))
            {
                padView.MoveCurrentTo(modelViewModel);
            }
        }
    }
}
